﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace BugSnapper
{
    public partial class frmHighLigtImage : Form
    {
        public frmHighLigtImage()
        {
            InitializeComponent();
        }
        private Logger Logger = new Logger();
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private string _imageToEdit = string.Empty;
        public string ImageToEdit
        {
            get
            {
                try
                {
                    return this._imageToEdit;
                }
                catch (Exception ex)
                {
                    this.Logger.ProcessError(ex);
                    return string.Empty;
                }
            }
            set
            {
                try
                {
                    this._imageToEdit = value;
                    this.pbEdit.Image = Image.FromFile(value);
                }
                catch (Exception ex)
                {
                    this.Logger.ProcessError(ex);
                }
            }
        }
        public string NewFilePath { get; set; }
        public HtmlElement SelectedElement { get; set; }

        public System.Drawing.Rectangle RectangleBounds { get; set; }

        private void frmHighLigtImage_Shown(object sender, EventArgs e)
        {
            try
            {
                Bitmap bmp = new Bitmap(this.ImageToEdit);
                if (bmp != null)
                {
                    Graphics gBmp = Graphics.FromImage(bmp);
                    gBmp.CompositingMode = CompositingMode.SourceCopy;

                    using (GetSelectionArea getSelectionArea = new GetSelectionArea())
                    {
                        getSelectionArea.ShowDialog();
                        this.RectangleBounds = getSelectionArea.RectangleBounds;
                        this.RectangleBounds = new Rectangle(this.RectangleBounds.X - Convert.ToInt32(AppGlobal.LineWidth), this.RectangleBounds.Y - ((this.Height - this.ClientSize.Height - 2 * ((this.Width - this.ClientSize.Width) / 2))), this.RectangleBounds.Width + Convert.ToInt32(AppGlobal.LineWidth), this.RectangleBounds.Height + Convert.ToInt32(AppGlobal.LineWidth));
                        Pen redPen = new Pen(System.Drawing.Color.Red, Convert.ToInt32(AppGlobal.LineWidth));
                        gBmp.DrawRectangle(redPen, this.RectangleBounds);
                        gBmp.Save();
                    }
                    try
                    {
                        bmp.Save(this.NewFilePath, System.Drawing.Imaging.ImageFormat.Bmp);
                    }
                    catch
                    { bmp.Save(this.NewFilePath); }
                    try
                    {
                        if (System.IO.File.Exists(this.NewFilePath))
                        {
                            this.SelectedElement.SetAttribute("src", this.NewFilePath);
                        }
                    }
                    catch { }
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
                this.Close();
            }
        }
    }
}
